import { Controller } from "../../mvc/core/Controller";
import { IAutoFightBriefController } from "./interfaces/IAutoFightBriefController";
import { DataEvent } from "../events/DataEvent";
import { TaskInfo } from "../model/TaskInfo";
import { MonstersUtil } from "../view/autoFight/MonstersUtil";
import { MapFileUtil } from "../scene/map/MapFileUtil";
import { BossConfig } from "../resource/BossConfig";
import { AutoFight } from "../view/autoFight/AutoFight";
import { ETaskStatus } from "../../../Message/Game/ETaskStatus";
import { IAutoFightBriefModule } from "../../../modules/interfaces/IAutoFightBriefModule";
import { AutoFightBriefModule } from "../../../modules/AutoFightBriefModule";
import { IAutoFightComplexModule } from "../../../modules/interfaces/IAutoFightComplexModule";
import { AutoFightComplexModule } from "../../../modules/AutoFightComplexModule";
import { IView } from "../../mvc/interfaces/IView";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { ServerCommand } from "../mvc/ServerCommand";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { CareerUtil } from "../view/common/util/CareerUtil";
import { ESkillUseType } from "../../../Message/Public/ESkillUseType";
import { SkillsUtil } from "../scene/skill/SkillsUtil";
import { SkillInfo } from "../model/SkillInfo";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { GameProxy } from "../mvc/GameProxy";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { ScreenOperation } from "../scene/ScreenOperation";
import { TSkill } from "../../../Message/Db/Tables/TSkill";
import { SkillRules } from "../rules/SkillRules";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SAutoFightSetting } from "../../../Message/Game/SAutoFightSetting";
//class AutoFightBriefController
    
    export  class AutoFightBriefController extends Controller implements IAutoFightBriefController
    {
       

        /* internal  */onAutoFightBriefOpenHandler(arg1: DataEvent): void
        {
            this.autoFightBrief.show();
            this.autoFightComplex.hide();
            return;
        }

        /* internal  */onTaskAddHandler(arg1: Object): void
        {
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc5=null;
            var loc6=null;
            var loc7=false;
            if (arg1 == null) 
            {
                return;
            }
            var loc1=arg1 as TaskInfo;
            if (loc1 != null) 
            {
                loc2 = loc1.getMonsterList();
                if (loc2 == null || loc2.length == 0) 
                {
                    return;
                }
                MonstersUtil.taskAddMonArr.length = 0;
                loc3 = 0;
                while (loc3 < loc2.length) 
                {
                    if ((loc5 = loc2[loc3]).mapId == MapFileUtil.mapID) 
                    {
                        MonstersUtil.taskAddMonArr.push(BossConfig.instance.getInfoByName(loc5.id));
                    }
                    ++loc3;
                }
                loc6 = AutoFight.localAutoFight.monsterArr;
                loc7 = false;
                loc3 = 0;
                while (loc3 < loc2.length) 
                {
                    var loc8=0;
                    var loc9=loc6;
                    for(loc4 of loc9) 
                    {
                        if (loc4 != loc2[loc3].id) 
                        {
                            continue;
                        }
                        loc2.splice(loc3, 1);
                        loc7 = true;
                        --loc3;
                        break;
                    }
                    if (loc7) 
                    {
                        break;
                    }
                    ++loc3;
                }
                loc8 = 0;
                loc9 = loc2;
                for(loc5 of loc9) 
                {
                    loc6.push(loc5.id);
                }
                AutoFight.localAutoFight.monsterArr = loc6;
                this.saveAndUpdateServer(false, false);
                if (!(this._autoFightComplex == null) && this._autoFightComplex.isHide == false) 
                {
                    this.autoFightComplex.attackPanel.update(true);
                }
            }
            return;
        }

        /* internal  */onTaskDelHandler(arg1: Object): void
        {
            var loc2=null;
            MonstersUtil.taskAddMonArr.length = 0;
            AutoFight.localAutoFight.monsterArr.length = 0;
            var loc1=cache.task.getTaskMonster(true);
            var loc3=0;
            var loc4=loc1;
            for(loc2 of loc4) 
            {
                AutoFight.localAutoFight.monsterArr.push(loc2.id);
            }
            return;
        }

        /* internal  */onTaskUpdateHandler(arg1: Array<any>): void
        {
            var loc2=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=0;
            var loc8=0;
            if (arg1 == null) 
            {
                return;
            }
            var loc1=false;
            var loc3=0;
            while (loc3 < arg1.length) 
            {
                if (arg1[loc3] == null) 
                {
                    break;
                }
                else 
                {
                    loc2 = arg1[loc3] as TaskInfo;
                    if (loc2.status == ETaskStatus._ETaskStatusHadCompleted) 
                    {
                        loc1 = true;
                        break;
                    }
                }
                ++loc3;
            }
            if (loc1) 
            {
                loc4 = cache.task.getTaskMonster(true);
                loc5 = AutoFight.localAutoFight.monsterArr;
                loc3 = 0;
                while (loc3 < loc4.length) 
                {
                    loc8 = 0;
                    while (loc8 < loc5.length) 
                    {
                        loc6 = loc4[loc3];
                        if (loc5[loc8] == loc6.id) 
                        {
                            loc4.splice(loc3, 1);
                            loc5.splice(loc8, 1);
                            break;
                        }
                        ++loc8;
                    }
                    ++loc3;
                }
                var loc9=0;
                var loc10=loc4;
                for(loc6 of loc10) 
                {
                    loc5.push(loc6.id);
                }
                AutoFight.localAutoFight.monsterArr = loc5;
            }
            return;
        }

        /* internal  */onGuideHangHandler(arg1: DataEvent): void
        {
            if (this.autoFightBrief.isHide) 
            {
                this.autoFightBrief.show();
            }
            else 
            {
                this.autoFightBrief.guideAutoFight();
            }
            return;
        }

        public get autoFightBrief(): IAutoFightBriefModule
        {
            if (this._autoFightBrief == null) 
            {
                this._autoFightBrief = new AutoFightBriefModule();
                this._autoFightBrief.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onBriefAddToStageHandler);
            }
            return this._autoFightBrief;
        }

        public get autoFightComplex(): IAutoFightComplexModule
        {
            if (this._autoFightComplex == null) 
            {
                this._autoFightComplex = new AutoFightComplexModule();
                this._autoFightComplex.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onComplexAutoAddToStageHandler);
            }
            return this._autoFightComplex;
        }

        /* internal  */onBriefAddToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            this.autoFightBrief.update(AutoFight.localAutoFight);
            this.autoFightComplex.attackPanel.update(true);
            if (cache.guide.autoFightGuide == true) 
            {
                this.autoFightBrief.guideAutoFight();
            }
            return;
        }

        /* internal  */onComplexAutoAddToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            this.autoFightComplex.update(AutoFight.localAutoFight);
            return;
        }

        protected /* override */ initView(): IView
        {
            return this.autoFightBrief;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateAutoFightSetting, this.onReceiveAutoFightInfoHandler);
            Dispatcher.addEventListener(EventName.SaveAutoFightInfo, this.onSaveAutoFightInfoHandler);
            Dispatcher.addEventListener(EventName.SaveDefaultAutoFight, this.onSaveDefaultInfoHandler);
            Dispatcher.addEventListener(EventName.SaveAndBeginToFight, this.onSaveAndBeginToFightHandler);
            Dispatcher.addEventListener(EventName.AutoFightEnd, this.onAutoFightEndHandler);
            Dispatcher.addEventListener(EventName.AutoFightKeyToFight, this.OnAutoFightKeyToFightHandler);
            NetDispatcher.addCmdListener(ServerCommand.SkillUpdate, this.onSaveWhenSkillUpdateHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdataHandler);
            Dispatcher.addEventListener(EventName.AutoFightComplexOpen, this.onAutoFightComplexOpenHandler);
            Dispatcher.addEventListener(EventName.AutoFightBriefOpen, this.onAutoFightBriefOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskAdd, this.onTaskAddHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskDel, this.onTaskDelHandler);
            Dispatcher.addEventListener(EventName.Guide_Hang, this.onGuideHangHandler);
            Dispatcher.addEventListener(EventName.SaveAutoFightInfoForOther, this.onSaveInfoForOtherHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleCareerUpdate, this.onRoleCareerUpdate);
            Dispatcher.addEventListener(EventName.SkillActiveRes, this.onSkillActiveResHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleCareerEnableUpdate, this.roleCareerEnableChangedHandler);
            return;
        }

        /* internal  */onSaveAutoFightInfoHandler(arg1: DataEvent): void
        {
            this.saveAndUpdateServer(true, false);
            return;
        }

        /* internal  */onSaveDefaultInfoHandler(arg1: DataEvent): void
        {
            this.saveAndUpdateServer(true, false);
            this.autoFightComplex.update(AutoFight.localAutoFight);
            return;
        }

        /* internal  */onSaveAndBeginToFightHandler(arg1: DataEvent): void
        {
            if (cache.copy.isHeroBattleCopy()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(200222));
                return;
            }
            this.saveAndUpdateServer(true, true);
            return;
        }

        /* internal  */onSaveInfoForOtherHandler(arg1: DataEvent): void
        {
            this.saveAndUpdateServer(false, false);
            if (this.autoFightBrief.isHide) 
            {
                if (!this.autoFightComplex.isHide) 
                {
                    this.autoFightComplex.update(AutoFight.localAutoFight);
                }
            }
            else 
            {
                this.autoFightBrief.update(AutoFight.localAutoFight);
            }
            return;
        }

        /* internal  */onRoleCareerUpdate(arg1: Object): void
        {
            if (CareerUtil.isGradeCareer(cache.role.entityInfo.career)) 
            {
                AutoFight.localAutoFight.clearAutoFightSkill();
                AutoFight.localAutoFight.alter = true;
                if (this._autoFightBrief && !this._autoFightBrief.isHide) 
                {
                    this._autoFightBrief.updateSkillList();
                }
                if (this._autoFightComplex && !this._autoFightComplex.isHide) 
                {
                    this._autoFightComplex.updateSkillList();
                }
                this.saveAndUpdateServer(true, false);
            }
            return;
        }

        /* internal  */roleCareerEnableChangedHandler(arg1: Object): void
        {
            if (this._autoFightBrief && !this._autoFightBrief.isHide) 
            {
                this._autoFightBrief.updateSkillList();
            }
            if (this._autoFightComplex && !this._autoFightComplex.isHide) 
            {
                this._autoFightComplex.updateSkillList();
            }
            return;
        }

        /* internal  */onSkillActiveResHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc4=0;
            var loc2=cache.skill.skillMaps;
            var loc3=[];
            loc4 = 0;
            while (loc4 < loc2.length) 
            {
                loc1 = loc2[loc4];
                if (loc1 && loc1.learned && loc1.useType == ESkillUseType._ESkillUseTypeInitiative && SkillsUtil.instance.isCanAddToAutoFightList(loc1.skillId, false)) 
                {
                    loc3.push(loc1);
                }
                ++loc4;
            }
            var loc5=AutoFight.localAutoFight.skillIdx;
            var loc6=AutoFight.localAutoFight.getSkillArr()[loc5];
            var loc7=AutoFight.localAutoFight.getSkillShowArr()[loc5];
            loc4 = 0;
            while (loc4 < loc7.length) 
            {
                if (loc2[loc4] != null) 
                {
                    loc1 = loc3[loc4] as SkillInfo;
                    if (loc1) 
                    {
                        loc6[loc4] = loc1.skillId;
                        loc7[loc4] = loc1.skillId;
                    }
                }
                ++loc4;
            }
            AutoFight.localAutoFight.alter = true;
            this.saveAndUpdateServer(false, false);
            return;
        }

        /* internal  */saveAndUpdateServer(arg1: Boolean=true, arg2: Boolean=false): void
        {
            AutoFight.currentAutoFight.copy(AutoFight.localAutoFight);
            if (cache.copy.isInboundlessSeaCopy() || GameMapUtil.curMapState.isCrossSea) 
            {
                AutoFight.localAutoFight.monsterArr.length = 0;
            }
            var loc1=AutoFight.localAutoFight.serializeString();
            if (AutoFight.localAutoFight.alter) 
            {
                AutoFight.localAutoFight.alter = false;
                GameProxy.autoFightProxy.saveAutoFightInfo(loc1);
            }
            if (arg1 == false) 
            {
                return;
            }
            if (arg2) 
            {
                if (RolePlayer.instance.swimming) 
                {
                    AutoFight.currentAutoFight.isAutoFight = false;
                    AutoFight.localAutoFight.isAutoFight = false;
                    MsgManager.showRollTipsMsg(Language.getString(34000));
                }
                else 
                {
                    ScreenOperation.interruptToAutoFight(this.gotoAutoFight);
                }
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(34001));
                Dispatcher.dispatchEvent(new DataEvent(EventName.SetAutoFightSuccess));
            }
            return;
        }

        /* internal  */gotoAutoFight(): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.BeginToFight));
            if (this._autoFightBrief && !this._autoFightBrief.isHide) 
            {
                this._autoFightBrief.updateAutoFightState();
            }
            if (this._autoFightComplex && !this._autoFightComplex.isHide) 
            {
                this._autoFightComplex.updateAutoFightState();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetAutoCall));
            return;
        }

        /* internal  */onSaveWhenSkillUpdateHandler(arg1: Object): void
        {
            var loc6=null;
            var loc7=null;
            var loc9=null;
            var loc10=0;
            var loc11=0;
            var loc12=0;
            var loc13=0;
            var loc14=0;
            var loc15=0;
            var loc1=cache.skill.skills;
            var loc2=[];
            var loc3=0;
            while (loc3 < loc1.length) 
            {
                if (loc1[loc3].useType == ESkillUseType._ESkillUseTypeInitiative && SkillsUtil.instance.isCanAddToAutoFightList(loc1[loc3].skillId, false)) 
                {
                    loc2.push(loc1[loc3]);
                }
                ++loc3;
            }
            var loc4;
            var loc5=(loc4 = arg1 as SkillInfo).position;
            var loc8;
            if ((loc8 = loc2.length) > 0 && loc8 <= 3 && loc4.tSkill.skillLevel == 1) 
            {
                loc6 = AutoFight.localAutoFight.getSkillArr()[0];
                loc7 = AutoFight.localAutoFight.getSkillShowArr()[0];
                loc10 = 0;
                while (loc10 < loc8) 
                {
                    loc9 = loc2[loc10] as TSkill;
                    loc6[loc10] = loc9.skillId;
                    loc7[loc10] = loc9.skillId;
                    ++loc10;
                }
            }
            else 
            {
                loc11 = AutoFight.localAutoFight.getSkillArr().length;
                loc13 = 0;
                while (loc13 < loc11) 
                {
                    loc6 = AutoFight.localAutoFight.getSkillArr()[loc13];
                    loc7 = AutoFight.localAutoFight.getSkillShowArr()[loc13];
                    loc12 = loc6.length;
                    loc14 = 0;
                    while (loc14 < loc12) 
                    {
                        if (loc6[loc14] != 0) 
                        {
                            if ((loc15 = SkillRules.getPostionById(loc6[loc14])) == loc5) 
                            {
                                loc6[loc14] = loc4.tSkill.skillId;
                                loc7[loc14] = loc4.tSkill.skillId;
                                break;
                            }
                        }
                        ++loc14;
                    }
                    ++loc13;
                }
            }
            AutoFight.localAutoFight.alter = true;
            this.saveAndUpdateServer(false, false);
            this.autoFightBrief.update(AutoFight.localAutoFight);
            return;
        }

        /* internal  */onReceiveAutoFightInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SAutoFightSetting;
            var loc2=loc1.jsStr;
            if (loc2 == "") 
            {
                if (!this._isSetDefault) 
                {
                    this._isSetDefault = true;
                    this.saveAndUpdateServer(false, false);
                }
            }
            else 
            {
                AutoFight.currentAutoFight.deserializeObj(loc2);
                AutoFight.localAutoFight.copy(AutoFight.currentAutoFight);
            }
            return;
        }

        /* internal  */onSceneUpdataHandler(arg1: DataEvent): void
        {
            this.findMonsters();
            if (!(this._autoFightComplex == null) && this._autoFightComplex.isHide == false) 
            {
                this.autoFightComplex.attackPanel.update(true);
            }
            else if (this._autoFightComplex != null) 
            {
                this.autoFightComplex.isSceneUpdate = true;
            }
            return;
        }

        /* internal  */findMonsters(): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            AutoFight.localAutoFight.monsterArr.length = 0;
            var loc1=AutoFight.localAutoFight.monsterArr;
            if (!(this._autoFightComplex == null) && !this._autoFightComplex.attackPanel.isTask) 
            {
                loc2 = cache.scene.getFightRefreshPoint();
            }
            else 
            {
                loc2 = cache.scene.getFightRefreshPoint();
                loc4 = cache.task.getTaskMonster(true);
                loc2 = MonstersUtil.getTaskMonsters(loc2, loc4);
            }
            var loc5=0;
            var loc6=loc2;
            for(loc3 of loc6) 
            {
                loc1[loc1.length] = loc3.code;
            }
            AutoFight.currentAutoFight.monsterArr = loc1.concat();
            return;
        }

        /* internal  */onAutoFightEndHandler(arg1: DataEvent): void
        {
            this._isBeginFight = false;
            AutoFight.currentAutoFight.isAutoFight = false;
            this.autoFightBrief.autoFightEnd();
            this.autoFightComplex.autoFightEnd();
            Dispatcher.dispatchEvent(new DataEvent(EventName.OverFight));
            return;
        }

        /* internal  */OnAutoFightKeyToFightHandler(arg1: DataEvent): void
        {
            if (cache.copy.isHeroBattleCopy()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(200222));
                return;
            }
            if (AutoFight.currentAutoFight.isAutoFight) 
            {
                AutoFight.currentAutoFight.isAutoFight = false;
                AutoFight.localAutoFight.isAutoFight = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.OverFight));
            }
            else if (RolePlayer.instance.swimming) 
            {
                MsgManager.showRollTipsMsg(Language.getString(34000));
            }
            else 
            {
                AutoFight.currentAutoFight.isAutoFight = true;
                AutoFight.localAutoFight.isAutoFight = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.BeginToFightShortcut));
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetAutoCall));
            return;
        }

        /* internal  */onAutoFightComplexOpenHandler(arg1: DataEvent): void
        {
            this.autoFightComplex.show();
            this.autoFightBrief.hide();
            return;
        }

        private /* var */_isSet: Boolean=false;

        private /* var */_isSetDefault: Boolean=false;

        private /* var */_isBeginFight: Boolean=false;

        private /* var */_autoFightComplex: IAutoFightComplexModule;

        private /* var */_autoFightBrief: IAutoFightBriefModule;
    }
